package com.example.testanimation.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.example.testanimation.R;
import com.example.testanimation.bean.Course;

import java.util.List;

public class CourseAdapter extends BaseAdapter {
    private List<Course> mList;
    private LayoutInflater mLayoutInflater;


    public CourseAdapter(List<Course> list, Context context) {
        mList = list;
        mLayoutInflater = LayoutInflater.from(context);
    }
    @Override
    public int getCount() {
        return mList.size();
    }
    public void updateData(List<Course> list) {
        mList.clear();
        mList = list;
        notifyDataSetChanged();
    }
    @Override
    public Course getItem(int position) {
        return mList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        CourseAdapter.ViewHolder viewHolder;
        if (convertView == null) {
            viewHolder = new CourseAdapter.ViewHolder();
            convertView = mLayoutInflater.inflate(R.layout.course_item, null);
            viewHolder.cnameText = convertView.findViewById(R.id.course_name);
            viewHolder.ccodeText = convertView.findViewById(R.id.course_code);
            viewHolder.cteacherText = convertView.findViewById(R.id.course_teacher);
            viewHolder.lhoursText = convertView.findViewById(R.id.learning_hours);
            convertView.setTag(viewHolder);

        } else {
            viewHolder = (CourseAdapter.ViewHolder) convertView.getTag();
        }
        Course course = getItem(position);
        if (course != null) {
            convertView.setTag(R.integer.friend_tag, course);
            viewHolder.cnameText.setText(String.valueOf(course.getCourse_name()));
            viewHolder.ccodeText.setText(course.getCourse_code());
            viewHolder.cteacherText.setText(course.getCourse_teacher());
            viewHolder.lhoursText.setText(course.getLearning_hours());

        }
        return convertView;
    }

    public static class ViewHolder {
        TextView cnameText;
        TextView ccodeText;
        TextView cteacherText;
        TextView lhoursText;
    }
}
